package com.lenovo.smarttraffic.ui.activity.stage5;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;

import java.util.ArrayList;
import java.util.List;

public class RoadAnalyzeActivity extends BaseActivity implements View.OnClickListener {

    private BarChart barChart;
    private BarData barData;
    private List<BarEntry> barEntries1 = new ArrayList<>();
    private List<BarEntry> barEntries2 = new ArrayList<>();
    private List<BarEntry> barEntries3 = new ArrayList<>();
    private List<BarEntry> barEntries4 = new ArrayList<>();
    private List<BarEntry> barEntries5 = new ArrayList<>();
    private List<BarEntry> barEntries6 = new ArrayList<>();
    private List<BarEntry> barEntries7 = new ArrayList<>();

    private int index = 0;
    private List<IBarDataSet> barDataSets = new ArrayList<>();
    private BarDataSet barDataSet1;
    private BarDataSet barDataSet2;
    private BarDataSet barDataSet3;
    private BarDataSet barDataSet4;
    private BarDataSet barDataSet5;
    private BarDataSet barDataSet6;
    private BarDataSet barDataSet7;
    private TextView mTvLegend1;
    private TextView mTvLegend2;
    private TextView mTvLegend3;
    private TextView mTvLegend4;
    private TextView mTvLegend5;
    private TextView mTvLegend6;
    private TextView mTvLegend7;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "路况分析");

        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        mTvLegend1.setOnClickListener(this);
        mTvLegend2.setOnClickListener(this);
        mTvLegend3.setOnClickListener(this);
        mTvLegend4.setOnClickListener(this);
        mTvLegend5.setOnClickListener(this);
        mTvLegend6.setOnClickListener(this);
        mTvLegend7.setOnClickListener(this);
    }

    private void initData() {
        //初始化显示


        //--------------------------------
        barEntries1.add(new BarEntry(0, 0));
        barDataSet1 = new BarDataSet(barEntries1, "1学院路");
        //--------------------------------
        barEntries2.add(new BarEntry(0, 0));
        barDataSet2 = new BarDataSet(barEntries2, "2联想路");
        //--------------------------------
        barEntries3.add(new BarEntry(0, 0));
        barDataSet3 = new BarDataSet(barEntries3, "3医院路");
        //--------------------------------
        barEntries4.add(new BarEntry(0, 0));
        barDataSet4 = new BarDataSet(barEntries4, "4幸福路");
        //--------------------------------
        barEntries5.add(new BarEntry(0, 0));
        barDataSet5 = new BarDataSet(barEntries5, "5环城快速路");
        //--------------------------------
        barEntries6.add(new BarEntry(0, 0));
        barDataSet6 = new BarDataSet(barEntries6, "6环城高速");
        //--------------------------------
        barEntries7.add(new BarEntry(0, 0));
        barDataSet7 = new BarDataSet(barEntries7, "7停车场");

        barDataSets.add(barDataSet1);
        barDataSets.add(barDataSet2);
        barDataSets.add(barDataSet3);
        barDataSets.add(barDataSet4);
        barDataSets.add(barDataSet5);
        barDataSets.add(barDataSet6);
        barDataSets.add(barDataSet7);

        barData = new BarData(barDataSets);
        barData.setDrawValues(false);

        barDataSet1.setColor(0xff1E90FF);
        barDataSet2.setColor(0xff66cccc);
        barDataSet3.setColor(0xff3CB371);
        barDataSet4.setColor(0xffFFA500);
        barDataSet5.setColor(0xffFF4500);
        barDataSet6.setColor(0xff8A2BE2);
        barDataSet7.setColor(0xffDB7093);

        Description description = new Description();
        description.setEnabled(false);
        barChart.setDescription(description);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawLabels(true);
        xAxis.setDrawGridLines(false);
        xAxis.setEnabled(false);

        YAxis axisLeft = barChart.getAxisLeft();
        axisLeft.setValueFormatter((value, axis) -> {
            switch ((int) value) {
                case 1:
                    return "畅通";
                case 2:
                    return "缓行";
                case 3:
                    return "一般拥堵";
                case 4:
                    return "中度拥堵";
                case 5:
                    return "严重拥堵";
                default:
                    return "状态";
            }
        });

        YAxis axisRight = barChart.getAxisRight();
        axisRight.setDrawGridLines(false);
        axisRight.setLabelCount(6, true);
        axisRight.setValueFormatter((value, axis) -> {
            switch ((int) value) {
                case 1:
                    return "1";
                case 2:
                    return "2";
                case 3:
                    return "3";
                case 4:
                    return "4";
                case 5:
                    return "5";
                default:
                    return "0";
            }
        });

        barChart.setTouchEnabled(false);
        barChart.setData(barData);
        //进行循环数据获取
        refresh.run();
    }

    /**
     * 根据状态显示
     */
    private Runnable refresh = new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i <= 7; i++) {
                int finalI = i;
                HttpUtils.post("get_road_status", new HttpUtils().add("RoadId", i).add("UserName", "user1"), jsonObject -> {
                    if (finalI == 1) {
                        barEntries1.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries1.size() > 7) {
                            barEntries1.remove(0);
                        }
                        barDataSet1.notifyDataSetChanged();
                    } else if (finalI == 2) {
                        barEntries2.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries2.size() > 7) {
                            barEntries2.remove(0);
                        }
                        barDataSet2.notifyDataSetChanged();
                    } else if (finalI == 3) {
                        barEntries3.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries3.size() > 7) {
                            barEntries3.remove(0);
                        }
                        barDataSet3.notifyDataSetChanged();
                    } else if (finalI == 4) {
                        barEntries4.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries4.size() > 7) {
                            barEntries4.remove(0);
                        }
                        barDataSet4.notifyDataSetChanged();
                    } else if (finalI == 5) {
                        barEntries5.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries5.size() > 7) {
                            barEntries5.remove(0);
                        }
                        barDataSet5.notifyDataSetChanged();
                    } else if (finalI == 6) {
                        barEntries6.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries6.size() > 7) {
                            barEntries6.remove(0);
                        }
                        barDataSet6.notifyDataSetChanged();
                    } else {
                        barEntries7.add(new BarEntry(index++, jsonObject.getInt("Status")));
                        if (barEntries7.size() > 7) {
                            barEntries7.remove(0);
                        }
                        barDataSet7.notifyDataSetChanged();
                    }

                    barData.notifyDataChanged();
                    barChart.notifyDataSetChanged();
                });
            }

            barChart.invalidate();
            index += 3;

            handler.postDelayed(refresh, 5000);
        }
    };

    @Override
    protected int getLayout() {
        return R.layout.activity_road_analyze;
    }

    private void initView() {
        barChart = findViewById(R.id.bc_road_state);
        mTvLegend1 = findViewById(R.id.tv_legend1);
        mTvLegend2 = findViewById(R.id.tv_legend2);
        mTvLegend3 = findViewById(R.id.tv_legend3);
        mTvLegend4 = findViewById(R.id.tv_legend4);
        mTvLegend5 = findViewById(R.id.tv_legend5);
        mTvLegend6 = findViewById(R.id.tv_legend6);
        mTvLegend7 = findViewById(R.id.tv_legend7);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_legend1:
                barDataSet1.setVisible(!barDataSet1.isVisible());
                break;
            case R.id.tv_legend2:
                barDataSet2.setVisible(!barDataSet2.isVisible());
                break;
            case R.id.tv_legend3:
                barDataSet3.setVisible(!barDataSet3.isVisible());
                break;
            case R.id.tv_legend4:
                barDataSet4.setVisible(!barDataSet4.isVisible());
                break;
            case R.id.tv_legend5:
                barDataSet5.setVisible(!barDataSet5.isVisible());
                break;
            case R.id.tv_legend6:
                barDataSet6.setVisible(!barDataSet6.isVisible());
                break;
            case R.id.tv_legend7:
                barDataSet7.setVisible(!barDataSet7.isVisible());
                break;
        }
        barChart.invalidate();
    }
}
